package cn.hunau.bookstore.filter;

import java.io.IOException;
import java.util.Objects;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 

/**
 * @author HuangBo
 *
 *         2022-01-20 16:25:11
 *
 */
@WebFilter(urlPatterns = { "/admin/*"})
public class LoginCheckFilter extends HttpFilter {
	@Override
	protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException { 
 		String matchValue = request.getServletPath(); 
		if ((matchValue.contains("/admin/admin/") && matchValue.contains(".html"))|| matchValue.contains(".do")) { 
			HttpSession session = request.getSession();
			Object admin = session.getAttribute("admin");
			System.out.println(admin);
			if (Objects.isNull(admin)) {
				response.sendRedirect("../login.html");
				System.out.println("redirect.....       xxxxxxxxxxxxxxxxx");
			} else {
				chain.doFilter(request, response);
			}
		} else {
			chain.doFilter(request, response);
			return;
		}
	}
}
